/*
 * 人员选择器模块
 * 2018年3月19日20:40:57
 * xwx-add
 */

(function($) {
	$.init();
	var contactsobj = server.user.getContactsInfo();
	var tit;
	var contactsinfo = {};

	$.plusReady(function() {

		var pdata = plus.webview.currentWebview().selectors;

		var navTit = plus.webview.currentWebview('typetit').typetit;
		//document.getElementById("bookTit").innerText = navTit; //设置选择器的导航标题
		tit = plus.webview.currentWebview('typetit').typetit == "人员选择" ? '1' : "2";
		
		server.user.getPeopleInfo(function(r) {
			if(r.errCode==0) {
				cdata = r.data;
				var ndata;
				var ntype = 1;
				if(pdata.length > 0) {
					for(var i = 0; i < cdata.length; i++) {
						cdata[i].check = false;
						for(var j = 0; j < pdata.length; j++) {
							if(cdata[i].id == pdata[j].id) {
								cdata[i].check = true;
							}
						}
					}
					ndata = cdata;
					ntype = 2;
				} else {
					ndata = cdata;
				}
				var a = document.getElementById("bookTit").innerText;
				var nums = ndata.length != 0 ? '('+ndata.length +')' : '';
				document.getElementById("bookTit").innerText = navTit + nums; // 设置通讯录人数
				//ndata = ndata.reverse();
				var w = new Vue({
					el: '#mui-userSelector',
					data: contactsinfo = {
						items: ndata,
						types: ntype
					}
				});
			}
		}, function(r) {
			mui.toast('获取服务器数据失败,请刷新重试');
		})

	});

	$('body').on('tap', '#btnBooking', function() {
		var userArr = [];
		var userObj = {}
		for(var i = 0; i < mui(".mui-input-group div").length; i++) {
			if(mui(".mui-input-group div")[i].getAttribute('data-status') == '1') {
				userObj = {
					"id": mui(".mui-input-group div")[i].getAttribute('data-id'),
					"name": mui(".mui-input-group div")[i].getAttribute('data-name') +"【"+  mui(".mui-input-group div")[i].getAttribute('data-deptName')+"】"
				}
				userArr.push(userObj)
			}
		}

		// 已选中的数据
		//console.log("已勾选数据:" + JSON.stringify(userArr));

		var parentPage = null;
		if(!parentPage) {
			parentPage = top.mui.openWindow('reservemeeting.html');
		};

		mui.fire(parentPage, "getUserselector", {
			"items": userArr,
			"type": tit
		});
	});

	//	获取切换选中名单
	$('body').on('change', '.mui-input-group input', function() {
		var status = this.checked ? "1" : ""; //自定义状态1为选中，''为未选中
		this.parentNode.setAttribute('data-status', status);
		getCheckNum();
	});

	// 显示隐藏保存按钮
	function getCheckNum() {
		var flag = true;
		for(var i = 0; i < mui(".mui-input-group div").length; i++) {
			if(mui(".mui-input-group div")[i].getAttribute('data-status') == '1') {
				document.getElementById('btnBooking').style.display = 'block';
				return;
			}
		}
		if(flag) {
			document.getElementById('btnBooking').style.display = 'none';
		}

	};

})(mui);